package com.example.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.example.demo.bean.Result;
import com.example.demo.common.Constent.STATUS;
import com.example.demo.config.Constent;
import com.example.demo.config.Constent.SEQUENCE;
import com.example.demo.dao.ShopMapper;
import com.example.demo.entity.Shop;
import com.example.demo.exception.SPIPException;
import com.example.demo.service.ShopService;
import com.example.demo.util.JwtUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @author 88377761
 *
 */
@RestController
@RequestMapping("/shop")
public class ShopController {

	@Autowired
	ShopMapper shopMapper;

	@Autowired
	ShopService shopService;

	@RequestMapping("/listByPage")
	@ResponseBody
	public Result listByPage(@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize, Shop shop) throws SPIPException {
		if (shop.getUserId() == null)
			throw new SPIPException("当前用户未登录");
		PageHelper.startPage(pageNum, pageSize);
		PageHelper.orderBy("r.is_current_shop DESC,s.id DESC");
		List<Shop> list = shopMapper.query(shop);
		for (Shop item : list) {
			item.setImgUrl(Constent.getImgUrl(SEQUENCE.SHOP, item.getId()));
		}
		return Result.getResultOk(new PageInfo<>(list));
	}

	@RequestMapping(value = "/getShop")
	@ResponseBody
	public Result getShop(Shop shop) {
		if (shop.getId() != null) {
			shop = shopMapper.selectByPrimaryKey(shop.getId());
			shop.setImgUrl(Constent.getImgUrl(SEQUENCE.SHOP, shop.getId()));
		}
		return Result.getResultOk(shop);
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result edit(@ModelAttribute Shop shop, MultipartFile file) throws SPIPException {
		shopService.edit(shop, file);
		return Result.getOk();
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	@ResponseBody
	public Result delete(@RequestBody Shop shop) throws SPIPException {
		String userId = JwtUtil.getUserId();
		shop.setUpdater(userId);
		shop.setUpdateDttm(new Date());
		shop.setStatus(STATUS.DELETE.getValue());
		shopMapper.updateByPrimaryKeySelective(shop);
		return Result.getOk();
	}

	@RequestMapping(value = "/setCurrentShop", method = RequestMethod.POST)
	@ResponseBody
	public Result setCurrentShop(@RequestBody Shop shop) throws SPIPException {
		shopService.setCurrentShop(JwtUtil.getUserId(), shop);
		return Result.getOk();
	}
}
